package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.vo.TDoctorVo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TDoctor;
import com.ruoyi.system.service.ITDoctorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 医生Controller
 * 
 * @author 杨一迪
 * @date 2024-12-26
 */
@Controller
@RequestMapping("/system/doctor")
public class TDoctorController extends BaseController
{
    private String prefix = "system/doctor";

    @Resource
    private ISysDeptService deptService;

    @Resource
    private ISysPostService postService;
    @Autowired
    private ITDoctorService tDoctorService;

    @RequiresPermissions("system:doctor:view")
    @GetMapping()
    public String doctor()
    {
        return prefix + "/doctor";
    }

    /**
     * 查询医生列表
     */
    @RequiresPermissions("system:doctor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TDoctor tDoctor)
    {
        startPage();
        List<TDoctor> list = tDoctorService.selectTDoctorList(tDoctor);
        List<TDoctorVo> tDoctorVos = new ArrayList<>();
        list.forEach(n ->{
            TDoctorVo tDoctorVo = new TDoctorVo();
            BeanUtils.copyProperties(n,tDoctorVo);
            SysDept sysDept = deptService.selectDeptById(n.getDepartmentId());
            SysPost sysPost = postService.selectPostById(n.getPostId());
            tDoctorVo.setDeptName(sysDept.getDeptName());
            tDoctorVo.setPostName(sysPost.getPostName());
            tDoctorVos.add(tDoctorVo);
        });
        return getDataTable(tDoctorVos);
    }

    /**
     * 导出医生列表
     */
    @RequiresPermissions("system:doctor:export")
    @Log(title = "医生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TDoctor tDoctor)
    {
        List<TDoctor> list = tDoctorService.selectTDoctorList(tDoctor);
        ExcelUtil<TDoctor> util = new ExcelUtil<TDoctor>(TDoctor.class);
        return util.exportExcel(list, "医生数据");
    }

    /**
     * 新增医生
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        List<SysPost> sysPosts = postService.selectPostList(new SysPost());
        mmap.put("depts",sysDepts);
        mmap.put("posts",sysPosts);
        return prefix + "/add";
    }

    /**
     * 新增保存医生
     */
    @RequiresPermissions("system:doctor:add")
    @Log(title = "医生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TDoctor tDoctor)
    {
        tDoctor.setUpdateTime(new Date());
        return toAjax(tDoctorService.insertTDoctor(tDoctor));
    }

    /**
     * 修改医生
     */
    @RequiresPermissions("system:doctor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TDoctor tDoctor = tDoctorService.selectTDoctorById(id);
        mmap.put("tDoctor", tDoctor);
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        List<SysPost> sysPosts = postService.selectPostList(new SysPost());
        mmap.put("depts",sysDepts);
        mmap.put("posts",sysPosts);
        return prefix + "/edit";
    }

    /**
     * 修改保存医生
     */
    @RequiresPermissions("system:doctor:edit")
    @Log(title = "医生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TDoctor tDoctor)
    {
        return toAjax(tDoctorService.updateTDoctor(tDoctor));
    }

    /**
     * 删除医生
     */
    @RequiresPermissions("system:doctor:remove")
    @Log(title = "医生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tDoctorService.softDeleteTDoctorByIds(ids));
    }
}
